package frontend;

import java.io.*;
import java.util.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Point;
import backend.*;

public class Main {

	protected Shell shell;
	private Text text;
	Label lblInputDirectory;
	private WMPlayer player;
	private Tree tree;
	private int framerate;
	private String videoName;
	private TimeLineAnalyzer tla;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Main window = new Main();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new FormLayout());
		
		SashForm sashForm = new SashForm(shell, SWT.NONE);
		FormData fd_sashForm = new FormData();
		fd_sashForm.left = new FormAttachment(0);
		fd_sashForm.bottom = new FormAttachment(100);
		fd_sashForm.right = new FormAttachment(100);
		sashForm.setLayoutData(fd_sashForm);
		
		lblInputDirectory = new Label(shell, SWT.NONE);
		fd_sashForm.top = new FormAttachment(lblInputDirectory, 6);
		lblInputDirectory.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
		FormData fd_lblInputDirectory = new FormData();
		fd_lblInputDirectory.bottom = new FormAttachment(0, 21);
		fd_lblInputDirectory.top = new FormAttachment(0);
		fd_lblInputDirectory.left = new FormAttachment(0);
		lblInputDirectory.setLayoutData(fd_lblInputDirectory);
		lblInputDirectory.setText("Input Directory");
		
		text = new Text(shell, SWT.BORDER);
		text.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				boolean result = readInput();
				if (result){
					lblInputDirectory.setBackground(
							SWTResourceManager.getColor(SWT.COLOR_GREEN));
				}else{
					lblInputDirectory.setBackground(
							SWTResourceManager.getColor(SWT.COLOR_RED));
				}
			}
		});
		FormData fd_text = new FormData();
		fd_text.bottom = new FormAttachment(sashForm, -6);
		fd_text.top = new FormAttachment(0);
		
		Composite composite = new Composite(sashForm, SWT.NONE);
		player = new WMPlayer(composite, SWT.NONE, SWT.NONE);
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		tree = new Tree(sashForm, SWT.BORDER);
		tree.addListener (SWT.MouseDown, new Listener () {
			public void handleEvent (Event event) {
				Point point = new Point (event.x, event.y);
				TreeItem item = tree.getItem (point);
				if (item != null) {
					System.out.println ("Mouse down: " + item);
					try{
						int start = (Integer)item.getData("start");
						int end = (Integer)item.getData("end");
						Double startTime = ((double)start)/framerate;
						Double endTime = ((double)end)/framerate;
//						for (int i = start; i <=end; i++){
//							player.goTo(((double)i)/framerate);
//						}
						player.goTo(startTime);
						player.playVideo();
						while(player.getVideoTime()<endTime){							
						}
						player.pauseVideo();
					}catch(NullPointerException e){						
					}
				}
			}
		});
		sashForm.setWeights(new int[] {1, 1});
		fd_text.left = new FormAttachment(lblInputDirectory, 4);
		fd_text.right = new FormAttachment(100);
		text.setLayoutData(fd_text);
	}
	
	/**
	 * Read input.
	 */
	public boolean readInput(){
		boolean result = true;
		String dir = this.text.getText();
		tla = new TimeLineAnalyzer();
		result = result & tla.readTimeLine(dir+"/timeline.txt");
		result = result & this.readVideoProperties(dir+"/videoproperties.txt");
		result = result & this.setVideo(dir+"/"+this.videoName);
		tla.buildTree(this.tree);
		return result;
	}
	
	/**
	 * Read the video properties from the file.
	 * @param filePath
	 */
	private boolean readVideoProperties(String filePath){
		File f = new File(filePath);
		try{
			Scanner scn = new Scanner(f);
			while(scn.hasNextLine()){
				String row = scn.nextLine();
				if (row.compareTo("@framerate") == 0){
					String val = scn.nextLine();
					this.framerate = Integer.parseInt(val);
				} else if(row.compareTo("@videoname")==0){
					this.videoName = scn.nextLine();
				}
			}
			scn.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * Read the video.
	 * @param filePath
	 * @return
	 */
	private boolean setVideo(String filePath){
		this.player.setVideo(filePath);
		this.player.pauseVideo();
		return this.player.hasVideo();
	}
}
